package com.everflourish.yeah100.utils;

import com.everflourish.yeah100.entity.statistics.ClassStatisticInfo;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class ClassStatisticsUtil {

    private String mPassRate;// 通过率
    private double mAvgScore; // 平均分
    private double mMaxScore = -Double.MAX_VALUE; // 最高分
    private double mMinScore = Double.MAX_VALUE; // 最低分
    private int m59Number = 0; // 60分以下
    private int m60Number = 0; // 60-70
    private int m70Number = 0; // 70-80
    private int m80Number = 0; // 80-90
    private int m90Number = 0; // 90-100
    private int maxNumber = 0;
    private float mBarMinValue = 0.02f;
    private int mTotalNumber = 0;
    private float mPassNumber = 0;

    public ClassStatisticsUtil(List<ClassStatisticInfo> classStatisticInfoList) {
        double tempAvgTotalScore = 0;
        for (ClassStatisticInfo c : classStatisticInfoList) {
            m59Number += StringUtil.parseInt(c.getSubtotalLessThan60(), 0);
            m60Number += StringUtil.parseInt(c.getSubtotal60To70(), 0);
            m70Number += StringUtil.parseInt(c.getSubtotal70To80(), 0);
            m80Number += StringUtil.parseInt(c.getSubtotal80To90(), 0);
            m90Number += StringUtil.parseInt(c.getSubtotal90To100(), 0);
            mTotalNumber += StringUtil.parseInt(c.getClassSize(), 0);
            mPassNumber += c.getPassRate();

            double tempMaxScore = StringUtil
                    .parseDouble(c.getHighestScore(), 0);
            double tempMinScore = StringUtil.parseDouble(c.getLowestScore(), 0);
            if (tempMaxScore > mMaxScore) {
                mMaxScore = tempMaxScore;
            }
            if (tempMinScore < mMinScore) {
                mMinScore = tempMinScore;
            }
            tempAvgTotalScore += StringUtil.parseDouble(c.getAvgScore(), 0);
        }
        if (classStatisticInfoList.size() != 0) {
            mAvgScore = tempAvgTotalScore / classStatisticInfoList.size();
            String avgScoreStr = mAvgScore + "";
            if (avgScoreStr.substring(avgScoreStr.indexOf(".")).length() > 2) {
                mAvgScore = StringUtil.parseDouble(
                        CommonUtil.formatDouble(mAvgScore, "#0.0"), 0);
            }
        }
        List<Integer> list = new ArrayList<Integer>();
        list.add(m59Number);
        list.add(m60Number);
        list.add(m70Number);
        list.add(m80Number);
        list.add(m90Number);
        for (Integer num : list) {
            if (num > maxNumber) {
                maxNumber = num;
            }
        }
    }

    /**
     * 获取及格率：格式100%
     */
    public String getmPassRate() {
        if (mPassNumber == 0) {
            mPassRate = "0%";
        } else {
            Double rassRate = (double) mPassNumber * 100
                    / (double) (mTotalNumber);
            DecimalFormat df = new DecimalFormat("#.00");
            mPassRate = df.format(rassRate) + "%";
        }
        return mPassRate;
    }

    public double getmAvgScore() {
        return mAvgScore;
    }

    public double getmMaxScore() {
        return mMaxScore;
    }

    public double getmMinScore() {
        return mMinScore;
    }

    public int getM59Number() {
        return m59Number;
    }

    public int getM60Number() {
        return m60Number;
    }

    public int getM70Number() {
        return m70Number;
    }

    public int getM80Number() {
        return m80Number;
    }

    public int getM90Number() {
        return m90Number;
    }

    public float get59Ratio() {
        float ratio = 0;
        ratio = (float) m59Number / (float) maxNumber;
        if (ratio < mBarMinValue) {
            ratio = (float) mBarMinValue + ratio;
        }
        return ratio;
    }

    public float get60Ratio() {
        float ratio = 0;
        ratio = (float) m60Number / (float) maxNumber;
        if (ratio < mBarMinValue) {
            ratio = (float) mBarMinValue + ratio;
        }
        return ratio;
    }

    public float get70Ratio() {
        float ratio = 0;
        ratio = (float) m70Number / (float) maxNumber;
        if (ratio < mBarMinValue) {
            ratio = (float) mBarMinValue + ratio;
        }
        return ratio;
    }

    public float get80Ratio() {
        float ratio = 0;
        ratio = (float) m80Number / (float) maxNumber;
        if (ratio < mBarMinValue) {
            ratio = (float) mBarMinValue + ratio;
        }
        return ratio;
    }

    public float get90Ratio() {
        float ratio = 0;
        ratio = (float) m90Number / (float) maxNumber;
        if (ratio < mBarMinValue) {
            ratio = (float) mBarMinValue + ratio;
        }
        return ratio;
    }
}
